package com.bingyuan.aipassproject.view.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bingyuan.aipassproject.R;
import com.bingyuan.aipassproject.utils.ToastUtil;
import com.bumptech.glide.Glide;

import java.util.ArrayList;

public abstract class SendGoldPopWindow {

    private PopupWindow popupWindow;
    private Activity context;
    private int selectType = 1;
    private String user_score;
    private String lock_score;
    private String drill_score = "";
    private String stake_score;

    private ArrayList<String> selectNumList;

    /**
     * 构造函数
     *
     * @param context
     */
    public SendGoldPopWindow(Activity context, String drill_score, String stake_score) {
        this.context = context;
        this.drill_score = drill_score;
        this.stake_score = stake_score;
    }

    /**
     * 构造函数
     *
     * @param context
     */
    public SendGoldPopWindow(Activity context, String user_score, String lock_score, String stake_score) {
        this.context = context;
        this.user_score = user_score;
        this.lock_score = lock_score;
        this.stake_score = stake_score;
    }

    /**
     * 显示警示框
     */
    public void show() {
        selectNumList = new ArrayList<>();

        View popView = View.inflate(context, R.layout.pop_send_gold, null);
        popupWindow = new PopupWindow(context);
        popupWindow.setAnimationStyle(R.style.popWindow_animation);//设置动画
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(null);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(popView);
        popupWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
        popupWindow.setOnDismissListener(() -> onDisMiss());

        ImageView ivClose = popView.findViewById(R.id.ivClose);
        RelativeLayout rlLeft = popView.findViewById(R.id.rlLeft);
        RelativeLayout rlRight = popView.findViewById(R.id.rlRight);
        TextView tvGoldNum = popView.findViewById(R.id.tvGoldNum);
        TextView tvLockGoldNum = popView.findViewById(R.id.tvLockGoldNum);
        TextView tvNumLeft = popView.findViewById(R.id.tvNumLeft);
        TextView tvNumRight = popView.findViewById(R.id.tvNumRight);
        ImageView ivType = popView.findViewById(R.id.ivType);
        LinearLayout llLeft = popView.findViewById(R.id.llLeft);
        ImageView ivSure = popView.findViewById(R.id.ivSure);
        //数字
        TextView tvZero = popView.findViewById(R.id.tvZero);
        TextView tvOne = popView.findViewById(R.id.tvOne);
        TextView tvTwo = popView.findViewById(R.id.tvTwo);
        TextView tvThree = popView.findViewById(R.id.tvThree);
        TextView tvFour = popView.findViewById(R.id.tvFour);
        TextView tvFive = popView.findViewById(R.id.tvFive);
        TextView tvSix = popView.findViewById(R.id.tvSix);
        TextView tvSeven = popView.findViewById(R.id.tvSeven);
        TextView tvEight = popView.findViewById(R.id.tvEight);
        TextView tvNine = popView.findViewById(R.id.tvNine);

        if (drill_score.length() == 0) {
            tvGoldNum.setText(user_score);
            tvLockGoldNum.setText(lock_score);
        } else {
            llLeft.setVisibility(View.INVISIBLE);
            Glide.with(context).load(R.drawable.icon_gold_bean).into(ivType);
            tvLockGoldNum.setText(drill_score);
        }

        tvNumLeft.setText(stake_score);
        tvNumRight.setText(stake_score);

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popView.findViewById(R.id.clLeft).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectType = 1;
                selectNumList = new ArrayList<>();
                //重置所有选择卡
                tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);


                rlLeft.setBackgroundResource(R.drawable.icon_pop_send_egg_select);
                rlRight.setBackgroundResource(R.drawable.icon_pop_send_egg_not_select);

            }
        });

        popView.findViewById(R.id.clRight).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectType = 2;
                selectNumList = new ArrayList<>();

                //重置所有选择卡
                tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);

                rlRight.setBackgroundResource(R.drawable.icon_pop_send_egg_select);
                rlLeft.setBackgroundResource(R.drawable.icon_pop_send_egg_not_select);
            }
        });

        ivSure.setOnClickListener(v -> {
            if (selectType == 1) {
                if (selectNumList.size() == 1) {
                    sureSelect(selectNumList,1);
                    popupWindow.dismiss();
                } else {
                    ToastUtil.showToast("请选择一个数字");
                }
            } else {
                if (selectNumList.size() == 2) {
                    sureSelect(selectNumList,0);
                    popupWindow.dismiss();
                } else {
                    ToastUtil.showToast("请选择两个数字");
                }
            }

        });
        tvZero.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("0")) {
                        selectNumList.remove("0");
                        tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("0");
                            tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("0")) {
                        selectNumList.remove("0");
                        tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("0");
                            tvZero.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvOne.setOnClickListener(v -> {
            if (selectType == 1) {
                if (selectNumList.contains("1")) {
                    selectNumList.remove("1");
                    tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                } else {
                    if (selectNumList.size() < 1) {
                        selectNumList.add("1");
                        tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                    } else {
                        ToastUtil.showToast("只能选择一个数字");
                    }
                }
            } else {
                if (selectNumList.contains("1")) {
                    selectNumList.remove("1");
                    tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                } else {
                    if (selectNumList.size() < 2) {
                        selectNumList.add("1");
                        tvOne.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                    } else {
                        ToastUtil.showToast("只能选择两个数字");
                    }
                }
            }
        });

        tvTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("2")) {
                        selectNumList.remove("2");
                        tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("2");
                            tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("2")) {
                        selectNumList.remove("2");
                        tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("2");
                            tvTwo.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("3")) {
                        selectNumList.remove("3");
                        tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("3");
                            tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("3")) {
                        selectNumList.remove("3");
                        tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("3");
                            tvThree.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("4")) {
                        selectNumList.remove("4");
                        tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("4");
                            tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("4")) {
                        selectNumList.remove("4");
                        tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("4");
                            tvFour.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvFive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("5")) {
                        selectNumList.remove("5");
                        tvFive.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("5");
                            tvFive.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("5")) {
                        selectNumList.remove("5");
                        tvFive.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("5");
                            tvFive.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvSix.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("6")) {
                        selectNumList.remove("6");
                        tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("6");
                            tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("6")) {
                        selectNumList.remove("6");
                        tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("6");
                            tvSix.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvSeven.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("7")) {
                        selectNumList.remove("7");
                        tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("7");
                            tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("7")) {
                        selectNumList.remove("7");
                        tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("7");
                            tvSeven.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvEight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("8")) {
                        selectNumList.remove("8");
                        tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("8");
                            tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("8")) {
                        selectNumList.remove("8");
                        tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("8");
                            tvEight.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

        tvNine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectType == 1) {
                    if (selectNumList.contains("9")) {
                        selectNumList.remove("9");
                        tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 1) {
                            selectNumList.add("9");
                            tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择一个数字");
                        }
                    }
                } else {
                    if (selectNumList.contains("9")) {
                        selectNumList.remove("9");
                        tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_not_select);
                    } else {
                        if (selectNumList.size() < 2) {
                            selectNumList.add("9");
                            tvNine.setBackgroundResource(R.drawable.icon_pop_send_egg_num_select);
                        } else {
                            ToastUtil.showToast("只能选择两个数字");
                        }
                    }
                }
            }
        });

    }

    /**
     * 消失
     */
    public abstract void onDisMiss();

    /**
     * 确定
     */
    public abstract void sureSelect(ArrayList<String> list,int type);

    public void dissMiss(){
        popupWindow.dismiss();
    }

    public boolean getShowType(){
        if (popupWindow!=null){
            return true;
        }else {
            return false;
        }
    }
}
